// pages/food/food.js
const db = wx.cloud.database()
Page({
  data: {
    totalMoney: 0,
    totalNum: 0,
    cartList: [], //购物车数据
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("options", options)
  },
  //点击增加
  add(e) {
    //console.log('点击了加号',e.currentTarget.dataset.id)
    let id = e.currentTarget.dataset.id
    let cartList = this.data.cartList
    // let totalMoney = this.data.totalMoney
    // let totalNum = this.data.totalNum
    cartList.forEach(item => {
      if (item._id == id) {
        item.num += 1;
        // totalMoney += item.price
        // totalNum += 1;
        if (cartList && cartList.length > 0) {
          //查询购物车数组里是否存在当前点击的菜品
          var result = cartList.find(cart => {
            return cart._id == id
          })
          console.log("当前点击的菜品是否存在于购物车里", result)
          if (!result) { //如果点击的菜品不在购物车里
            cartList.push(item)
          } else {
            result.num = item.num
          }
        } else {
          cartList.push(item)
        }

        // this.setData({
        //   totalMoney: this.data.totalMoney += item.price,
        //   totalNum: this.data.totalNum += 1,
        //   cartList:this.data.cartList.push(item)
        // })
      }
    })
    console.log('遍历购物车列表', this.data.cartList)
    this.setData({
      //totalMoney, //totalMoney:totalMoney
      //totalNum,
      cartList,
    })
    this.getTotal()
    wx.setStorageSync('cart', cartList)
  },
  //点击减少
  sub(e) {
    console.log('点击了减号', e.currentTarget.dataset.id)
    let id = e.currentTarget.dataset.id
    let cartList = this.data.cartList
    cartList.forEach((item, index) => {
      if (item._id == id) {
        if (item.num > 0) {
          item.num -= 1;
          //console.log("当前点击的菜品是否存在于购物车里",result)
          if (item.num == 0) { //如果点击的菜品数量为0，则移除购物车
            cartList.splice(index, 1) //删除下标为index的元素
          }
        } else {
          wx.showToast({
            title: '数量为0',
            icon: 'none'
          })
        }
      }

    })
    this.setData({
      //totalMoney, //totalMoney:totalMoney
      //totalNum,
      cartList,
    })
    this.getTotal()
    wx.setStorageSync('cart', cartList)
  },

  //计算总价格和总数量
  getTotal() {
    let cartList = this.data.cartList
    let totalMoney = 0
    let totalNum = 0

    cartList.forEach(item => {
      totalMoney += item.num * item.price
      totalNum += item.num
    })
    this.setData({
      totalNum,
      totalMoney,
    })
  },

  //删除购物车里的一条数据
  closeCartItem(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let cartList = this.data.cartList
    //从购物车数组删除当前菜品
    cartList.splice(index, 1)
    this.setData({
      cartList,
    })
    //重新计算总价格，总数量
    this.getTotal()
    //把更新后的数据重新缓存
    wx.setStorageSync('cart', cartList)
  },

  //去菜品列表页
  goFood() {
    wx.navigateTo({
      url: '/pages/food/food',
    })
  },
  //去支付页
  goOrder() {
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //获取购物车缓存数据
    let cartList = wx.getStorageSync('cart') || []
    console.log('本地缓存的购物车数据', cartList)
    this.setData({
      cartList: cartList
    })
    this.getTotal()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})